/*
 * $Id: SessionTrackingListener.java 42 2013-04-17 15:33:59Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 42 $
 * $Date: 2013-04-17 15:33:59 +0000 (Wed, 17 Apr 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.web.support;

import com.sci.logisticsmap.service.GeodataService;
import com.sci.logisticsmap.service.UserLogService;
import com.sci.logisticsmap.service.support.SessionBriefcase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * The class {@code SessionTrackingListener} implements both the servlet context listener
 * and the http session listener.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 42 $
 */
public class SessionTrackingListener implements ServletContextListener, HttpSessionListener {
    /**
     * The logger.
     */
	private static final Logger logger;

    /**
     * The Geodata service.
     */
    private GeodataService geodataService;
    /**
     * The user log service.
     */
    private UserLogService userLogService;

    /**
     * The static initialization block.
     */
    static {
	    logger = LoggerFactory.getLogger(SessionTrackingListener.class);
    }

    /*
     * (non-Javadoc)
     *
     * @see ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent sce) {
        //logger.debug("SessionTrackingListener init called...");
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
        geodataService = applicationContext.getBean(GeodataService.class);
        userLogService = applicationContext.getBean(UserLogService.class);
    }

    /*
     * (non-Javadoc)
     *
     * @see ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent sce) {
        SessionBriefcase.INSTANCE.clear();
    }

    /*
     * (non-Javadoc)
     *
     * @see HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent event) {
        HttpSession session = event.getSession();
        //logger.debug("Session '{}' created", session.getId());
        SessionBriefcase.INSTANCE.init(session);
    }

    /*
     * (non-Javadoc)
     *
     * @see HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent event) {
        try {
            HttpSession session = event.getSession();
            //logger.debug("Session '{}' destroyed", session.getId());
            SessionBriefcase.INSTANCE.remove(session);

            geodataService.deleteBySessionId(session.getId());
            userLogService.addSessionDestroyed(session.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
